import("../../../fastlane/Fastfile")

desc "Update swift package commit"
lane :update_swift_package_commit do
  project_file_location = '../SPMIntegration.xcodeproj/project.pbxproj'

  old_kind_line = "kind = branch;"
  new_kind_line = "kind = revision;"

  replace_string_in_path(old_kind_line, new_kind_line, project_file_location)

  commit_hash = last_git_commit[:commit_hash]
  old_branch_line = "branch = main;"
  new_revision_line = "revision = #{commit_hash};"

  replace_string_in_path(old_branch_line, new_revision_line, project_file_location)
end

def replace_string_in_path(old_string, new_string, path)
  backup_extension = '.bck'
  sed_regex = 's|' + old_string + '|' + new_string + '|'  
  sh("sed", '-i', backup_extension, sed_regex, path)
end